

/*--------------------------------------------desk--------------------------------------*/

function desk(){
	this.oWrap = $$('#wrap');                   //全局包装
	this.oDesk = $$('#desk');					//主桌面
	this.oTask = $$('#task');					//任务栏
	this.oLeft = $$('#leftBar');				//左菜单栏
	this.oBg = $$('#bgWindow');					//个性化设置背景图
	this.oDeskContent = $$('#deskContent');		//桌面内容
	this.deskShowCount = '';					//桌面个数
	this.aDeskCount = 6;    					//九宫格个数
	this.iconMarRgt = 50;
	this.iconMarTop = 20;
	this.deskPdgTop = 50;  						//图标桌面上变局
	this.deskPdgLeft = 100; 					//图标桌面左边距
	this.minDis = 50;    						//碰撞时交换图标的最小距离
	this.className = zh_className;      		//css的类样式
	this.addIconCon = zh_addIcon;       		//桌面icon添加应用
	this.aDeskIconContentThreeView = zh_deskIconVary; //桌面图票数组，多界面
	this.aDeskIconContent = zh_deskIcon; 		//桌面图标数组
	this.iconMenuContent = zh_iconMenu;			//图标右键菜单
	this.deskMenuContent = zh_deskMenu;  		//桌面右键菜单内容数组
	this.argMent = 'col';						//图标排列方式'row'：横向，'col'：纵向，其他值：自由排列
	this.aDesk = [];							//桌面数组区域
	this.iNowDesk = 1;							//默认显示桌面1
	this.aCheckDesk = [];						//顶部桌面导航
	this.aDeskIcon =[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]];	//每个桌面的图标
	this.iconMenu = [];	                		//桌面图标菜单
	this.deskMenu = '';                 		//桌面右键菜单栏
	this.addIcon = [];
	this.deskMenuMain = [];             		//桌面右键内容列表
	this.deskMenuList1 = [];            		//桌面第一个内容右滑动右边显示内容
	this.deskMenuList2 = [];					//桌面第二个内容右滑动右边显示内容
	this.iconMenuMain = [[],[],[],[],[],[],[],[],[],[],[],[],[]];    	//图标右键内容列表
	this.iconMenuList = [[],[],[],[],[],[],[],[],[],[],[]]; 	//图标第一个内容右滑动右边显示内容
}
desk.prototype = {
	init:function (array,deleteAxios){//初始化
		var _this = this;
		this.createDesk();
		this.createLeftApp();/*生成左侧图标*/
		// this.createFixApp(); //生成两个固定的APP协同工单和统计报表
		this.createApp(array);/*生成后台传过来的图标*/
		this.events(deleteAxios);
		console.log(array);
		return true;
	},
	initFixApp:function (array,deleteAxios){//初始化固定APP应用
		var _this = this;
		this.createDesk();
		this.createAppTest(array);/*生成后台传过来的图标*/
		return true;
	},
	createDesk:function (){						//创建桌面区域
		var oFrag = document.createDocumentFragment();
		for(var i=0;i<6;i++){
			var desk = document.createElement('div');
			desk.id = 'desk'+(i+1);
			desk.className = this.className.iconDesk;
			desk.index = i;
			desk.setAttribute('ondrop',"drop(event)");				//拖放，放置目标源
			desk.setAttribute('ondragover',"allDrop(event)");
			this.aDesk.push(desk);
			oFrag.appendChild(desk);
		}
		this.oDeskContent.appendChild(oFrag);
	},
	createApp:function(array){						//生成桌面图标
		for(var i=0;i<array.length;i++){
			for(var j=0;j<array[i].length;j++){
				var newIcon = new createIcon(this.oDesk,this.aDesk[i],this.oTask,array[i][j]);
				newIcon.init();
				newIcon.icon.index = j;
				this.aDeskIcon[i].push(newIcon.icon);
			}
			this.aDeskIcon[i].index = i;
		}
	},
	createAppTest:function(array){						//生成桌面图标（数据交换平台测试用）
		for(var i=0;i<array.length;i++){
			for(var j=0;j<array[i].length;j++){
				let counte = array[i][j];
				var newIcon = new createIcon(this.oDesk,this.aDesk[counte.desktop],this.oTask,array[i][j]);
				newIcon.init();
				newIcon.icon.index = j;
				this.aDeskIcon[i].push(newIcon.icon);
			}
			this.aDeskIcon[i].index = i;
		}
	},
	createFixApp:function(json){						//生成固定桌面图标
		console.log(this.aDesk)
		console.log(this.oDesk)
		console.log(this.oTask)
		var newIcon = new createIcon(this.oDesk,this.aDesk[1],this.oTask,json);
		// newIcon.init();
	},
	createLeftApp:function (){					//生成左侧图标

		/*特殊处理左侧图标中用户的信息*/
		for(var i = 0;i<zh_leftIcon.length;i++){
			new createIcon(this.oDesk,this.oLeft,this.oTask,zh_leftIcon[i]).init();
		}
		// console.log(this.oLeft)
		changeLeftIcon('leftIcon0');/*更改图标，主页显示*/
		return true;
	},
	createNewApp:function(){					//生成新的App
		//在这里判断是在那个区域一个区域添加新应用，还是在第二个Region.
		//判断第一个区域是否超过六个APP，如果超过，就在第二个Region添加。
		//以此类推，以下五个区域都是按照这种方法。
		console.log(this.aDesk.length)
		for(let i=0; i<this.aDesk.length; i++){
			if(this.aDesk[i].children.length < 7){
				let newIcon = new createIcon(this.oDesk,this.aDesk[i],this.oTask,zh_deskIconJiuNew[createIconFlag]?zh_deskIconJiuNew[createIconFlag]:zh_deskIconJiuNew[0]);
				newIcon.init();
				this.aDeskIcon[i].push(newIcon.icon);
				return ;
			}
		}
	},
	createNewApp2:function(json){					//生成新的App
		//在这里判断是在那个区域一个区域添加新应用，还是在第二个Region.
		//判断第一个区域是否超过六个APP，如果超过，就在第二个Region添加。
		//以此类推，以下五个区域都是按照这种方法。
		// console.log(this.aDesk.length)
		if(this.aDesk.length){
			for(let i=0; i<this.aDesk.length; i++){
				if(this.aDesk[i].children.length < 6){
					let newIcon = new createIcon(this.oDesk,this.aDesk[i],this.oTask,json);
					newIcon.init();
					this.aDeskIcon[i].push(newIcon.icon);
					// console.log(this.aDesk[i].id)
					if(this.aDesk[i].id){
						return this.aDesk[i].id;
					}else{
						return 'desk1'
					}

				}
			}
		}else{
			return 'desk1'
		}
	},
	events:function (deleteAxios){			//事件
		var _this = this;
		var i=0;
		window.onresize = function (){
			console.log('onresize')
		};
		function wheel(ev){
			var ev = ev||event;
			var iNow = _this.iNowDesk;
			var flag = false;
			if(ev.wheelDelta){
				flag = ev.wheelDelta>0 ? true : false;
			}
			if(ev.detial){
				flag = ev.detail<0 ? true : false;
			}
			if(flag){
				iNow++;
				if(iNow > 5){
					iNow = 1;
				}
				_this.iNowDesk = iNow;
				_this.checkDesk(iNow-1);
			}else{
				iNow--;
				if(iNow < 1){
					iNow = 5;
				}
				_this.iNowDesk = iNow;
				_this.checkDesk(iNow-1);
			}
			if(ev.preventDefault){
				ev.preventDefault();
			}
			return false;
		};
		document.oncontextmenu = function (ev){
			var ev = ev||event;
			var X = ev.clientX;
			var Y = ev.clientY;
			_this.deskContextmenu();
			_this.deskMenu.style.left = X +'px';
			_this.deskMenu.style.top = Y+'px';
			return false;
		};

		this.oLeft.oncontextmenu = function (ev){
			var ev = ev||event;
			ev.cancelBubble = true;
			return false;
		};
		addEvent(document,'click',function (){
			if(_this.deskMenu){
				_this.deskMenu.style.display = 'none';
			}
			if(_this.addIcon[_this.iNowDesk-1]){
				_this.addIcon[_this.iNowDesk-1].style.display = 'none';
			}
		})

		for(i=0;i<this.aCheckDesk.length;i++){
			this.aCheckDesk[i].index = i;
			this.aCheckDesk[i].onmousedown = function (ev){
				var ev = ev||event;
				var This = this;
				var X = ev.clientX;
				var Y = ev.clientY;
				this.onmouseup = function (ev){
					var ev = ev||event;
					if(ev.clientX == X && ev.clientY == Y){
						_this.checkDesk(This.index);
					}
				};
				ev.cancelBubble = true;
				return false;
			}
		}

		/*--------------------------图标事件---------------------------------*/

		for(i=0;i<this.aDeskIcon.length;i++){
			for(var j=0;j<this.aDeskIcon[i].length;j++){
				this.addIconEvent(i,j,deleteAxios);

			}
		}
	},

	addIconEvent:function (deskIndex,objIndex,deleteAxios){	//图标事件
		var _this = this;
		this.aDeskIcon[deskIndex][objIndex].oncontextmenu=function (ev){
			var ev = ev||event;
			var iL = 0;
			if(this.isOpen == 'yes'){
				_this.iconContextmenu(this,deskIndex,deleteAxios);
				var moreLi = _this.iconMenu[deskIndex].getElementsByTagName('li')[1];
				_this.iconMenu[deskIndex].style.left = ev.clientX+'px';
				_this.iconMenu[deskIndex].style.top = ev.clientY+'px';
				iL = viewW()-_this.iconMenu[deskIndex].offsetWidth;

				if(ev.clientX>=iL){
					_this.iconMenu[deskIndex].style.left = iL+'px';
					moreLi.className = _this.className.iconMenuLeft;
				}else{
					if(ev.clientX>=iL-130){
						moreLi.className = _this.className.iconMenuLeft;
					}else{
						moreLi.className = _this.className.iconMenuRight;
					}

				}
			}
			else{
				if(_this.iconMenu[deskIndex]){
					_this.iconMenu[deskIndex].style.display = 'none';
				}
				if(_this.deskMenu){
					_this.deskMenu.style.display = 'none';
				}
			}
			if(_this.iconMenu[deskIndex]){
				_this.iconMenu[deskIndex].style.zIndex = zh_winzIndex;
			}
			if(_this.addIcon[_this.iNowDesk-1]){
				_this.addIcon[_this.iNowDesk-1].style.display = 'none';
			}
			ev.cancelBubble = true;
			return false;
		};
	},
	checkDesk:function (num){					//选择桌面
		for(var i=0;i<this.aDesk.length;i++){
			this.aCheckDesk[i].className = '';
			if(i>num){
				fnMove(this.aDesk[i],{left:viewW(),opacity:0});
			}
			if(i<num){
				fnMove(this.aDesk[i],{left:-viewW(),opacity:0});
			}
		}
		this.iNowDesk = num+1;
//		fnMove(this.aDesk[num],{left:0,opacity:100})/*这个设定， 一点击就会把整个窗口往左归0，先注释*/
		this.aCheckDesk[num].className = this.className.activeNum;
	},
	setSize:function (){						//设置桌面大小
		this.oWrap.style.height = viewH()+'px';
		this.oWrap.style.width = viewW()+'px';
		this.oDesk.style.height = viewH()+'px';
		this.oDesk.style.width = viewW()+'px';
		this.oTask.style.top = (viewH()-35)+'px';

		for(var i = 0 ; i < this.aDesk.length; i++){
			this.aDesk[i].style.height = (viewH()-140)+'px';
			this.aDesk[i].style.width = (viewW()-50)+'px';
		}
	},
	arrangeIcon:function (argMent){				//图标自动排列
		var oneWidth = (this.iconMarRgt+this.aDeskIcon[0][0].offsetWidth);
		var oneHeight = (this.iconMarTop+this.aDeskIcon[0][0].offsetHeight);
		for(var i=0;i<this.aDesk.length;i++){
			if(argMent == 'row'){
				var row = parseInt((this.aDesk[i].offsetHeight)/oneHeight);
				var col = Math.ceil(this.aDeskIcon[i].length/row);
				for(var j=0;j<this.aDeskIcon[i].length;j++){

					fnMove(this.aDeskIcon[i][j],{
						'top':((j%row)*oneHeight+this.deskPdgTop),
						'left':(parseInt(j/row)*oneWidth+this.deskPdgLeft),
						'opacity':100
					});
				}
			}
			else if(argMent == 'col'){
				var col = parseInt((this.aDesk[i].offsetWidth)/oneWidth);
				var row = Math.ceil(this.aDeskIcon[i].length/col);
				for(var j=0;j<this.aDeskIcon[i].length;j++){
					fnMove(this.aDeskIcon[i][j],{
						'top':(parseInt(j/col)*oneHeight+this.deskPdgTop),
						'left':((j%col)*oneWidth+this.deskPdgLeft),
						'opacity':100
					})
				}
			}
		}
	},
	moveArrangeIcon:function (obj,num){			//拖动交换位置
		var _this = this;
		var dis = 0;
		var isCol = false;
		obj.onmouseup = function (){
			for(var n=0;n<_this.aDeskIcon[num].length;n++){
				if(_this.aDeskIcon[num][n] != obj && _this.aDeskIcon[num][n].isOpen == 'yes'){
					isCol = isCollision(obj,_this.aDeskIcon[num][n]);
					dis = distance(obj,_this.aDeskIcon[num][n]);

					if(isCol && dis <= obj.offsetWidth/2){
						var index2 = _this.aDeskIcon[num][n].index;
						var length = _this.aDeskIcon[num].length;

						_this.aDeskIcon[num].splice(obj.index,1);

						_this.aDeskIcon[num].splice(index2,0,obj);

						for(var i=0;i<_this.aDeskIcon[num].length;i++){
							_this.aDeskIcon[num][i].index = i;
						}
						break;
					}
				}
			}
			_this.arrangeIcon(_this.argMent);
		}
		addEvent(document,'mouseup',function (){

			_this.arrangeIcon(_this.argMent);
		})
	},
	contextMenu:function (parent,MenuContent,deskId,cur){	//创建右键菜单
		console.log("1123");
		var oDiv = document.createElement('div');
		oDiv.className = this.className.menu;
		for(var i=0;i<MenuContent.length;i++){
			if(!oUl){
				var oUl = document.createElement('ul');
			}
			var li = document.createElement('li');
			li.innerHTML = MenuContent[i].innerHTML;
			li.id = deskId + MenuContent[i].id;
			// if(MenuContent[i].id == 'showdesk'){
			// 	li.innerHTML = '<span>'+MenuContent[i].innerHTML+'</span>';  //添加两横线的样式
			// }
			if(MenuContent == this.deskMenuContent){
				this.deskMenuMain.push(li);
			}
			if(MenuContent == this.iconMenuContent){
				this.iconMenuMain[cur].push(li);
			}
			this.contextList(li,MenuContent[i],deskId,cur);
			oUl.appendChild(li);
		}
		oDiv.appendChild(oUl);
		parent.appendChild(oDiv);
		return oDiv;
	},
	contextList:function (parent,MenuContent,deskId,cur){	//生成子菜单
		var _this = this;
		for(var i=0;i<MenuContent.length;i++){
			if(!parent.ul){
				parent.ul = document.createElement('ul');
				parent.innerHTML = MenuContent[0].innerHTML;
				parent.id = deskId + MenuContent[0].id;
				i++;
			}
			if(MenuContent == this.iconMenuContent[1]){
				parent.className = this.className.iconMenuRight;
			}

			var li = document.createElement('li');
			li.innerHTML = MenuContent[i].innerHTML;
			li.id = deskId + MenuContent[i].id;
			li.index = i-1;
			if(MenuContent == this.deskMenuContent[0] || MenuContent == this.deskMenuContent[1]){
				parent.className = this.className.deskMenuRight;
				if(MenuContent == this.deskMenuContent[0]){
					this.deskMenuList1.push(li);
				}
				if(MenuContent == this.deskMenuContent[1]){
					this.deskMenuList2.push(li);
				}
			}
			if( MenuContent == this.iconMenuContent[1]){
				this.iconMenuList[cur].push(li);
			}
			this.contextList(li,MenuContent[i],deskId);
			parent.ul.appendChild(li);
			parent.appendChild(parent.ul);
		}
	},
	iconContextmenu: function (obj,num,deleteAxios){		//app右键菜单
		var _this = this;
		if(!_this.iconMenu[num]){
			_this.iconMenu[num] = _this.contextMenu(_this.aDesk[num],_this.iconMenuContent,_this.aDesk[num].id,num);
		}else{
			_this.iconMenu[num].style.display = 'block';
		}
		if(_this.deskMenu){
			_this.deskMenu.style.display = 'none';
		}
		//alert(_this.iconMenu[0]+';'+num)
		_this.iconMenuMain[num][0].onclick = function(ev){
			_this.openWindow(obj,num);

		}
		_this.iconMenuMain[num][2].onclick = function(){	//删除应用
			_this.delIcon(obj,num,deleteAxios);
			obj.style.display = 'none';
		}

		// for(var i=0;i<_this.iconMenuList[num].length;i++){
		// 	if(_this.iconMenuList[num][i].className != _this.className.iconMenuCur){
		// 		//调整大小
		// 		_this.iconMenuList[num][i].onclick = function (ev){
		// 			_this.iconMenu[num].style.display = 'none';
		// 		};
		// 	}
		// }
		_this.iconMenuList[num][0].onclick = function(ev){    //图标右键调整大小的调小。
			//如果点击这个小的就存回1到后台。
			obj.style.width = 90 + 'px';
			obj.style.height = 90 + 'px';
			obj.firstChild.style.marginTop = 0 + 'px';
			_this.iconMenu[num].style.display = 'none';
			console.log(obj.json)
			axios.post(zuulHost+'/m_manage/userAppMenu/update?access_token='+accessToken,{
				"id": obj.json.id,
				"appCode": obj.json.appCode,
				"pos": obj.json.pos,
				"title": obj.json.title,
				"src": obj.json.src,
				"webSrc": obj.json.webSrc,
				"iwidth": 1300,
				"iheight": 500,
				"ileft": 110,
				"region": obj.json.region,
				"pictureSize": 1,
				"desktop": obj.json.desktop
				})
				.then(function(res){
					console.log(res.data);
					obj.json = res.data;
				})
				.catch(function(err){
				console.log(err);
			});
		};
		_this.iconMenuList[num][1].onclick = function(ev){
			//计算这个区域里的九宫格数目
			let jsonAll = obj.parentNode.children;
			var sum = 0;
			for(let i=0; i<jsonAll.length;i++){
				try{
					var a =jsonAll[i].json.pictureSize;
				}catch(e){

				}
				if(a){
					sum+=parseInt(a);
				}else{
					sum+='b';
				}
			}
			console.log(sum);
			if(sum > 9){
				alert('没有足够的位置了，请腾出位置后再变中。')
				_this.iconMenu[num].style.display = 'none';
			}else{
				obj.style.width = 185 + 'px';
				obj.style.height = 90 + 'px';
				obj.firstChild.style.marginTop = 0 + 'px';
				_this.iconMenu[num].style.display = 'none';
				axios.post(zuulHost+'/m_manage/userAppMenu/update?access_token='+accessToken,{
					"id": obj.json.id,
					"appCode": obj.json.appCode,
					"pos": obj.json.pos,
					"title": obj.json.title,
					"src": obj.json.src,
					"webSrc": obj.json.webSrc,
					"iwidth": 1300,
					"iheight": 500,
					"ileft": 110,
					"region": obj.json.region,
					"pictureSize": 2,
					"desktop": obj.json.desktop
					})
					.then(function(res){
						console.log(res.data);
						obj.json = res.data
					})
					.catch(function(err){
					console.log(err);
				});
			}
		};
		_this.iconMenuList[num][2].onclick = function(ev){
			//计算这个区域里的九宫格数目
			let jsonAll = obj.parentNode.children;
			var sum = 0;
			for(let i=0; i<jsonAll.length;i++){
				try{
					var a =jsonAll[i].json.pictureSize;
				}catch(e){

				}
				if(a){
					sum+=parseInt(a);
				}else{
					sum+='b';
				}
			}
			console.log(sum);
			if(sum > 6){
				alert('没有足够的位置了，请腾出位置后再变大。')
				_this.iconMenu[num].style.display = 'none';
			}else{
				obj.style.width = 185 + 'px';
				obj.style.height = 185 + 'px';
				obj.firstChild.style.marginTop = 50 + 'px';
				_this.iconMenu[num].style.display = 'none';
				axios.post(zuulHost+'/m_manage/userAppMenu/update?access_token='+accessToken,{
					"id": obj.json.id,
					"appCode": obj.json.appCode,
					"pos": obj.json.pos,
					"title": obj.json.title,
					"src": obj.json.src,
					"webSrc": obj.json.webSrc,
					"iwidth": 1300,
					"iheight": 500,
					"ileft": 110,
					"region": obj.json.region,
					"pictureSize": 4,
					"desktop": obj.json.desktop
					})
					.then(function(res){
						console.log(res.data);
						obj.json = res.data
					})
					.catch(function(err){
					console.log(err);
				});
			}
		};
		_this.iconMenu[num].onclick = function (){
			var ev = ev||event;
			ev.cancelBubble = true;
		};
		document.onclick = function (){
			_this.iconMenu[num].style.display = 'none';
		};
	},
	fnAddIcon:function (){
		var _this = this;
		if(!this.addIcon[this.iNowDesk-1]){
			this.addIcon[this.iNowDesk-1] = this.contextMenu(this.aDesk[this.iNowDesk-1],this.addIconCon,this.aDesk[this.iNowDesk-1].id);
		}else{
			this.addIcon[this.iNowDesk-1].style.display = 'block';
		}
		this.addIcon[this.iNowDesk-1].onclick = function (){
			this.style.display = 'none';
		};

	},
	openWindow:function (obj,deskIndex){		//右键打开应用
		this.iconMenu[deskIndex].style.display = 'none';
		console.log(deskIndex);
		console.log(obj);
		var Window = $$('#'+this.aDesk[deskIndex].id+'window'+obj.id);
		if(Window){
			Window.style.left = obj.offsetLeft+'px';
			Window.style.top = obj.offsetTop+'px';
			return;
		}
		new newWindow(this.oDesk,this.oTask,this.aDesk[deskIndex].id+'window'+obj.id,obj.json).init();
	},
	moveIcon:function (obj,arr,toArr,curDesk,toDesk){	//移动到,交换数据
		var _this = this;
		var index1 = curDesk.index;
		var index2 = toDesk.index;
		for(var i=0;i<arr.length;i++){
			if(arr[i] == obj){
				arr.splice(i,1);
			}
		}
		toArr.splice(toArr.length-1,0,obj);
		for(var i=0;i<arr.length;i++){
			arr[i].index = i;
			_this.addIconEvent(index1,i);
		}
		for(var i=0;i<toArr.length;i++){
			toArr[i].index = i;
			_this.addIconEvent(index2,i);
		}
		toDesk.appendChild(obj);
		_this.arrangeIcon(_this.argMent);
	},
	delIcon:function (obj,deskIndex,deleteAxios){			//删除应用
		var _this = this;
		fnMove(obj,{opacity:0},function (){
			_this.aDesk[deskIndex].removeChild(obj);
			console.log(obj.ids); //APP删除的ID
		})
		for(var i=0;i<this.aDeskIcon[deskIndex].length;i++){
			if(this.aDeskIcon[deskIndex][i] == obj){
				this.aDeskIcon[deskIndex].splice(i,1);
			}
		}
		for(var i=0;i<this.aDeskIcon[deskIndex].length;i++){
			this.aDeskIcon[deskIndex][i].index = i;
		}
		this.iconMenu[deskIndex].style.display = 'none';
		console.log(this.iconMenu[deskIndex]);
		//删除对应的APP在数据库
		axios.post(zuulHost+'/m_manage/userAppMenu/delete/'+obj.ids+'?access_token='+accessToken)
			.then(function(res){
				console.log(res);
			})
			.catch(function(err){
			console.log(err);
      	});
        console.log(err);
		console.log(deleteAxios)
	},
	deskContextmenu:function (){				//桌面右键菜单
		var _this = this;
		if(!this.deskMenu){
			this.deskMenu = this.contextMenu(this.oDesk,this.deskMenuContent,this.oDesk.id,1);
		}else{
			this.deskMenu.style.display = 'block';
		}
		if(_this.addIcon[_this.iNowDesk-1]){
			_this.addIcon[_this.iNowDesk-1].style.display = 'none';
		}
		this.deskMenu.onclick = function (ev){
			var ev = ev||event;
			ev.cancelBubble = true;
		};
		this.deskMenu.style.zIndex = zh_winzIndex;
		//暂时不要右键新增APP
		// this.deskMenuMain[0].onclick = function (){
		// 	//添加一些转入值
		// 	let titles = '添加新应用';
		// 	_this.createNewApp();
		// 	_this.events();
		// 	_this.deskMenu.style.display = 'none';
		// };
		this.deskMenuMain[0].onclick = function (){
			_this.showDesk();
		};
		this.deskMenuMain[1].onclick = function (){
			_this.setBg()
		};
	},
	iconType:function (num,typeClass,size){		//查看
		for(var i=0;i<this.deskMenuList1.length;i++){
			this.deskMenuList1[i].className = '';
		}
		this.deskMenuList1[num].className = this.className.typeChecked;
		this.deskMenu.style.display = 'none';
		for(var i=0;i<this.aDesk.length;i++){
			for(var j=0;j<this.aDeskIcon[i].length;j++){
				this.aDeskIcon[i][j].className = typeClass;
				this.aDeskIcon[i][j].style.height = size +'px';
				this.aDeskIcon[i][j].style.width = size +'px';
				this.arrangeIcon(this.argMent);
			}
		}
	},
	argType:function (num,type){				//排列方式
		for(var i=0;i<this.deskMenuList2.length;i++){
			this.deskMenuList2[i].className = '';
		}
		this.deskMenuList2[num].className = this.className.typeChecked;
		this.deskMenu.style.display = 'none';
		this.argMent = type;
		this.arrangeIcon(this.argMent);
	},
	showDesk:function (){						//显示桌面
		var aWindow = getByClass(this.oDesk,this.className.thisWindow);
		var aTask = getByClass(this.oTask,this.className.taskMenu);
		for(var i=0;i<aWindow.length;i++){
			aWindow[i].style.display = 'none';
			fnMove(aWindow[i],{opacity:0})
		}
		for(var i=0;i<aTask.length;i++){
			fnMove(aTask[i],{width:90,opacity:100});
			aTask[i].style.display = 'block';
		}
		$$('#bgWindow').style.display = 'none';
		if(this.deskMenu!=''){
			this.deskMenu.style.display = 'none';
		}
		changeLeftIcon('leftIcon0');
		/*更换图标*/
	},
	reLoad:function (){							//刷新
		for(var i=0;i<this.aDeskIcon[this.iNowDesk-1].length;i++){
			this.aDeskIcon[this.iNowDesk-1][i].style.opacity =0;
			fnMove(this.aDeskIcon[this.iNowDesk-1][i],{opacity:100});
			this.arrangeIcon(this.argMent);
		}
		this.deskMenu.style.display = 'none';
	},
	setBg:function (){							//主题设置
		setBg(this.oBg);
		this.deskMenu.style.display = 'none';
	},
	drag:function (obj){						//导航条拖拽
		obj.onmousedown = function(ev){
			var ev = ev||event;
			var disX = ev.clientX - this.offsetLeft;
			var disY = ev.clientY - this.offsetTop;
			if(obj.setCapture){
				obj.setCapture();
			}
			this.style.zIndex = zh_winzIndex;
			document.onmousemove = function (ev){
				var ev = ev||event;
				var iL = ev.clientX - disX;
				var iT = ev.clientY - disY;
				if(iL<0){
					iL = 0;
				}
				if(iL>viewW()-obj.offsetWidth){
					iL = viewW() - obj.offsetWidth;
				}
				if(iT<10){
					iT = 10;
				}
				if(iT>=(viewH() - obj.offsetHeight)){
					iT = viewH() - obj.offsetHeight;
				}
				obj.style.left = iL+'px';
				obj.style.top = iT+'px';
			};
			document.onmouseup = function (){
				document.onmousemove = document.onmouseup = null;
				if(obj.releaseCapture){
					obj.releaseCapture();
				}
			}
			return false;
		}
	},

};

/*---------------------------------------------APP那些小应用---------------------------------------*/
// 全局变量createIconFlag
var createIconFlag = 1;
function createIcon(winParent,appParent,oTask,json){	//APP
	this.winParent = winParent;      			//整体界面
	this.appParent = appParent;      			//界面容器
	this.oTask = oTask;    						//任务栏
	this.json = json;      						//配置信息
	this.iconId = json.id;
	this.imgSrc = json.src;
	this.title = json.title;
	this.isOpen = json.isOpen || 'yes';
	this.iconClass = json.className||zh_className;
	this.isClick = false;
	this.iconPos = json.pos||'desk';
	this.isDel = json.isDel||true;
	this.zIndex = 0;
	this.appCode = json.appCode || '';
//	console.log(json);

}
createIcon.prototype = {
	init:function (){							//初始化
		var _this = this;
		this.create();
		// this.setAppzIndex();
		this.drag(_this.icon);
	},
	create:function (){							//创建app，并且把app塞到桌面去
		this.icon = document.createElement('div');
		this.iconImg = document.createElement('div');
		this.img = document.createElement('img');
		this.icontitle = document.createElement('a');
		this.icoDelete = document.createElement('div');

		this.iconImg.title = this.title;
		this.iconImg.setAttribute('draggable','false');
		this.img.src = this.imgSrc;
		this.img.setAttribute('draggable','false');
		this.icontitle.innerHTML = this.title;
		this.icon.json = this.json;
		this.icon.isOpen = this.isOpen;
		if(this.icon.json.pos =='new'){
			this.icon.id = this.appParent.id+this.iconId +createIconFlag;
			createIconFlag += 1;
		}else{
			this.icon.id = this.appParent.id+this.iconId;
		}
		this.icon.setAttribute('secondId',this.appCode);//添加第二id属性
		this.icon.setAttribute('draggable','true');       //给APP加上拖放功能
		this.icon.setAttribute('ondragstart',"drag(event)");
		this.icon.setAttribute('isClick',this.isClick);
		this.icon.className = this.iconClass.icon;   //默认的大小图标 icon
		this.iconImg.className = this.iconClass.iconImg;
		this.icontitle.className = this.iconClass.icontitle;
		this.icoDelete.className = this.iconClass.iconDelete;

		if(this.iconPos == 'left'){/*这里是生成左侧图标*/
			if(this.iconId ==='leftIcon1'){/*特殊处理用户头像*/
				console.log(this);
				// console.log(userName);
				this.icon.className = this.iconClass.leftIcon;
				this.iconImg.className = this.iconClass.leftIconImg;
			}else{
				this.icon.className = this.iconClass.leftIcon;
				this.iconImg.className = this.iconClass.leftIconImg;
			}
		}
		if(this.iconPos == 'jiu' || this.iconPos == 'new'){
			this.icon.className = this.iconClass.jiuIcon;
			this.iconImg.className = this.iconClass.jiuIconImg;
			this.icon.region = this.json.region;
			this.icon.ids = this.json.id; //通过这里赋值axios异步传过来的id值，用来删除应用的数据库使用。
			//调取后台给回来的1，2，4的值来判断，窗口的小中大状态。
			var state = this.json.pictureSize;
			if(state==2){
				//因为后台原因，所以规则需要改变一下，1是小图标 ，2 是中多标， 4是大图标。
				this.icon.style.width = '185px';
				this.icon.style.height = '90px';
				// this.icon.style.boxSizing = 'border-box';
				// this.icon.style.paddingTop = 15 + 'px';
//				console.log(this.icon.firstChild)
			}else if(state==1){
				this.icon.style.width = '90px';
				this.icon.style.height = '90px';
				// this.icon.firstChild.style.marginTop = 15 + 'px';
			}else if(state==4){
				this.icon.style.width = '185px';
				this.icon.style.height = '185px';
				this.icon.style.boxSizing = 'border-box';
				this.icon.style.paddingTop = 50 + 'px';
				// this.icon.firstChild.style.marginTop = 50 + 'px';
			}else{
				this.icon.style.width = '90px';
				this.icon.style.height = '90px';
			}

			// this.icon.className = this.iconClass.leftIcon;
			// this.iconImg.className = this.iconClass.leftIconImg;
		}
		if(this.iconId !='leftIcon1'){/*头像特殊处理*/
				this.iconImg.appendChild(this.img);
				this.icon.appendChild(this.iconImg);
		}
		else{
			let name = document.createElement("p");
			let allName = document.createElement("span");
			allName.className='user_name_all';
			try {
				allName.innerHTML =userNameAll;
				name.innerHTML =userName;
			} catch (e) {/*获取失败*/
				console.log(e);
				  axios.get(zuulHost+'/uaa/user/getCurrentUserInfo?access_token='+accessToken,{
        			}).then(function(res){
		            console.log(res);
					if(res.data.data.name!=''){
						userNameAll = res.data.data.name/*全名，全局变量*/
						userName = res.data.data.name.substr(res.data.data.name.length-1,1);
						console.log(userName);/*最后一个字,全局变量*/
						allName.innerHTML =userNameAll;
						name.innerHTML =userName;
					}
		        })
			}
			name.className='user_name';
			this.icon.appendChild(name);
			this.icon.appendChild(allName);
		}
		if(this.iconPos == 'desk'){
			this.icon.appendChild(this.icontitle);
		}
		if(this.iconPos == 'jiu' || this.iconPos == 'new'){
			this.icon.appendChild(this.icontitle);
		}
		this.icon.appendChild(this.icoDelete);
		this.appParent.appendChild(this.icon);
	},
	setAppzIndex:function (){					//改变app层级
		setStyle(this.icon,{'z-index':zh_appzIndex++});
		this.zIndex = zh_appzIndex;
	},
	drag:function (obj){						//APP拖拽;
		var _this = this;
		obj.onmousedown = function(ev){
			var oDate1 = new Date();
			var time1 = oDate1.getTime();
			_this.setAppzIndex();
			var ev = ev || event;
			var X = ev.clientX;
			var Y = ev.clientY;
			var disX = X - parseInt(getStyle(_this.icon,'left'));
			var disY = Y - parseInt(getStyle(_this.icon,'top'));
			if (obj.setCapture) {
				obj.setCapture();
			}
			document.onmouseup = function(ev) {/*左键打开引用*/
				var ev = ev||event;
				var oBg = $$('#bgWindow')
				console.log(_this);
				var newwin = new newWindow(_this.winParent,_this.oTask,_this.appParent.id+'window'+_this.iconId,_this.json);
				if(ev.clientX == X && ev.clientY == Y){
					if(!$$('#'+_this.appParent.id+'window'+_this.iconId)){	/*一个app只允许打开一个窗口*/
						console.log("wwwwww");
						var appNow = obj.setAttribute('isClick',false);
					}
					if(obj.getAttribute('isClick') == 'false' && obj.isOpen == 'yes'){
						console.log(obj);
						if(obj.id == 'leftBarleftIcon5'){/*打开设置*/
							setBg(oBg);
							return false;
						}
						
						if(obj.id == 'leftBarleftIcon6'){/*退出*/
							out();
							return false;
						}
						if(obj.id == 'leftBarleftIcon0'){/*首页*/
							console.log(desk.prototype);
 							let showDesk = new desk();
 							showDesk.showDesk();
							return false;
						}


						obj.setAttribute('isClick',true);
						obj.onclick = function (){/*这里是打开窗口的关键*/
							if(obj.id=='leftBarleftIcon1'){/*个人主页，layer的弹窗，管他*/
//									var newwin = new newWindow(_this.winParent,_this.oTask,_this.appParent.id+'window'+_this.iconId,_this.json).initFix();
									layerDiyMyself();
							}else{
									var newwin = new newWindow(_this.winParent,_this.oTask,_this.appParent.id+'window'+_this.iconId,_this.json).init();
									changeLeftIcon(_this.json.id)/*更换左侧图标*/
							}

						};
					}
					else{
						if(obj.isOpen == 'yes'){
							obj.onclick = function(){
								 newwin.showTwo(newwin,_this);
								return true;
							};
						}
					}
				}
				else{
					if(_this.isOpen == 'yes'){
						console.log("onclick");
						obj.onclick = null;
					}
				}
				if (obj.releaseCapture) {
					obj.releaseCapture();
				}
				document.onmouseup = document.onmousemove = null;
			}
			// ev.cancelBubble = true;
		}
	}
};

/*-------------------------------------------window窗口-------------------------------------*/

function newWindow(oParent,oTask,id,json){
	this.oParent = oParent;										//父窗口
	this.oTask = oTask;											//任务栏
	this.windowId = id;
	this.windowClass = json.className||zh_className;			//窗口样式
	this.timer1 = null;											//关闭时等待动画效果完成
	this.timer2 = null;											//检测是否为当前窗口
	this.isMax = json.isMax||'mid';								//窗口状态其值为min,mid,max;
	this.zIndex = 0;
	this.iLeft = json.iLeft||300;
	this.iTop = json.iTop=(json.iTop<=20?20:json.iTop)||51;		//窗口top值
//	this.iWidth = json.iWidth||1000;
//	this.iHeight = json.iHeight||500;
	this.iWidth = json.iWidth||960;
	this.iHeight = json.iHeight||570;
	this.webSrc = json.webSrc||'./other/wanli/index.html';
	this.title = json.title||'万年历';
	this.minWidth = json.minWidth||250;
	this.minHeight = json.minHeight||50;
	this.appCode= json.appCode||'';
	this.flag = false;
	console.log(this);
	console.log(json);
};


/******************************************************正常窗口打开**************************************************************/

newWindow.prototype = {
	init:function (){							//初始化窗口;
		let isFive = false;
		var _this = this;
		this.createWindow();
		this.createTask();
		this.show();
		this.currentWindow();
		this.events();
		this.timer2 = setInterval(function (){
			_this.checkCurWindow();
		},30);
		//初始化时，所有窗口最大化
		/*这里做出修改，当appcode是那5个的时候，就不进行最大化了。*/
		for (let i = 0; i < special_appCode.length; i++) {
			if(_this.appCode===special_appCode[i]){
				isFive = true;
				return false;
			}
		}
		console.log(isFive);
		if(!isFive){
			setTimeout(function(){
				console.log(document.getElementsByClassName('windowMaximize'));
				var winmax = document.getElementsByClassName('windowMaximize')[0];
				winmax.click();
			},10)
		}
	},
	initFix:function (){							//初始化窗口;
		let isFive = false;
		var _this = this;
		this.createWindowFix();
		this.createTask();
		this.show();
		this.currentWindow();
		this.events();
		this.timer2 = setInterval(function (){
			_this.checkCurWindow();
		},30);
		//初始化时，所有窗口最大化
		/*这里做出修改，当appcode是那5个的时候，就不进行最大化了。*/
		for (let i = 0; i < special_appCode.length; i++) {
			if(_this.appCode===special_appCode[i]){
				isFive = true;
				return false;
			}
		}
		console.log(isFive);
		if(!isFive){
			setTimeout(function(){
				var winmax = document.getElementsByClassName('windowMaximize')[0];
				winmax.click();
			},40)
		}

	},
	createWindow:function(){					//创建窗口;
		zh_windowCount+=50;
		this.thisWindow = document.createElement('div');
		this.windowContainer = document.createElement('div');
		this.windowMenu = document.createElement('div');
		this.windowMenuContent = document.createElement('div');
		this.windowControl = document.createElement('div');
		this.windowMinimize = document.createElement('div');
		this.windowMaximize = document.createElement('div');
		this.windowClose = document.createElement('div');
		this.windowContent = document.createElement('div');
		this.windowFrame = document.createElement('iframe');
		this.contentMask = document.createElement('div');
		this.windowMenuContent.innerHTML = this.title;
		this.windowFrame.src = this.webSrc;
	    this.windowAppCode = this.appCode;

		this.thisWindow.innerHTML =
		'<div class='+this.windowClass.windowTopResize+'></div>'+
		'<div class='+this.windowClass.windowLeftResize+'></div>'+
		'<div class='+this.windowClass.windowRightResize+'></div>'+
		'<div class='+this.windowClass.windowBottomResize+'></div>'+
		'<div class='+this.windowClass.windowTlResize+'></div>'+
		'<div class='+this.windowClass.windowTrResize+'></div>'+
		'<div class='+this.windowClass.windowBrResize+'></div>'+
		'<div class='+this.windowClass.windowBlResize+'></div>';

		this.thisWindow.id = this.windowId;
		this.thisWindow.className = this.windowClass.thisWindow;
		this.windowContainer.className = this.windowClass.windowContainer;
		this.windowMenu.className = this.windowClass.windowMenu;
		this.windowMenuContent.className = this.windowClass.windowMenuContent;
		this.windowControl.className = this.windowClass.windowControl;
		this.windowMinimize.className = this.windowClass.windowMinimize;
		this.windowMaximize.className = this.windowClass.windowMaximize;
		this.windowClose.className = this.windowClass.windowClose;
		this.windowContent.className = this.windowClass.windowContent;
		this.windowFrame.className = this.windowClass.windowFrame;
		this.contentMask.className = this.windowClass.contentMask;

		this.windowControl.appendChild(this.windowMinimize);
		this.windowControl.appendChild(this.windowMaximize);
		this.windowControl.appendChild(this.windowClose);
		this.windowMenu.appendChild(this.windowMenuContent);
		this.windowMenu.appendChild(this.windowControl);
		this.windowContainer.appendChild(this.windowMenu);
//		this.windowContent.appendChild(this.contentMask);
		this.windowContent.appendChild(this.windowFrame);
		this.windowContainer.appendChild(this.windowContent);
		this.thisWindow.appendChild(this.windowContainer);
		this.oParent.appendChild(this.thisWindow);
	},
	createWindowFix:function(){					//创建固定窗口;
		zh_windowCount+=50;
		this.thisWindow = document.createElement('div');
		this.windowContainer = document.createElement('div');
		this.windowMenu = document.createElement('div');
		this.windowMenuContent = document.createElement('div');
		this.windowControl = document.createElement('div');
		this.windowMinimize = document.createElement('div');
		this.windowMaximize = document.createElement('div');
		this.windowClose = document.createElement('div');
		this.windowContent = document.createElement('div');
		this.windowFrame = document.createElement('iframe');
		this.contentMask = document.createElement('div');
		this.windowMenuContent.innerHTML = this.title;
		this.windowFrame.src = this.webSrc;
	    this.windowAppCode = this.appCode;

		this.thisWindow.innerHTML =
		'<div class='+this.windowClass.windowTopResize+'></div>'+
		'<div class='+this.windowClass.windowLeftResize+'></div>'+
		'<div class='+this.windowClass.windowRightResize+'></div>'+
		'<div class='+this.windowClass.windowBottomResize+'></div>'+
		'<div class='+this.windowClass.windowTlResize+'></div>'+
		'<div class='+this.windowClass.windowTrResize+'></div>'+
		'<div class='+this.windowClass.windowBrResize+'></div>'+
		'<div class='+this.windowClass.windowBlResize+'></div>';

		this.thisWindow.id = this.windowId;
		this.thisWindow.className = this.windowClass.thisWindow;
		this.windowContainer.className = this.windowClass.windowContainer;
		this.windowMenu.className = this.windowClass.windowMenu;
		this.windowMenuContent.className = this.windowClass.windowMenuContent;
		this.windowControl.className = this.windowClass.windowControl;
		this.windowMinimize.className = this.windowClass.windowMinimize;
		this.windowMaximize.className = this.windowClass.windowMaximize;
		this.windowClose.className = this.windowClass.windowCloseFix;
		this.windowContent.className = this.windowClass.windowContent;
		this.windowFrame.className = this.windowClass.windowFrame;
		this.contentMask.className = this.windowClass.contentMask;

//		 this.windowControl.appendChild(this.windowMinimize);
//		 this.windowControl.appendChild(this.windowMaximize);
		this.windowControl.appendChild(this.windowClose);
		this.windowMenu.appendChild(this.windowMenuContent);
		this.windowMenu.appendChild(this.windowControl);
		this.windowContainer.appendChild(this.windowMenu);
		this.windowContent.appendChild(this.contentMask);
		this.windowContent.appendChild(this.windowFrame);
		this.windowContainer.appendChild(this.windowContent);
		this.thisWindow.appendChild(this.windowContainer);
		this.oParent.appendChild(this.thisWindow);
	},
	events:function(){							//事件
		var _this = this;

		this.drag(this.windowMenu);
		this.thisWindow.onmousewheel = function (ev){
			var ev = ev||event;
			ev.cancelBubble = true;
			return false;
		};
		if (this.thisWindow.addEventListener) {
			this.thisWindow.addEventListener('DOMMouseScroll',
				function (ev){
					var ev = ev||event;
					if(ev.preventDefalut){
						ev.preventDefalut();
					}
					return false;
				}
			, false);
		}
		this.resizeWindow();

		/**************************标题栏事件*************************/

		addEvent(this.windowMinimize,'click',function (){		//最小化按钮
			_this.minimize();
		});
		addEvent(this.windowMinimize,'click',function (){		//最小化按钮
			_this.minimize();
		});
		addEvent(this.windowMaximize,'click',function (){//放大缩小按钮
			console.log('maxmin');
			if(_this.isMax == 'mid'){
				_this.maximize();
			}else if(_this.isMax == 'max'){
				_this.narrow();
			}
			_this.setWindowzIndex();                            //提升窗口顶层
		});
		addEvent(this.windowClose,'click',function (){			//关闭按钮
			_this.closeWindow();
		});
		addEvent(this.windowMenu,'dblclick',function (){		//双击
//			console.log(_this);
			if(_this.windowId==='leftBarwindowleftIcon1'){/*双击了用户*/
				return false;
			}
			if(_this.isMax == 'mid'){
				_this.maximize();
			}else if(_this.isMax == 'max'){
				_this.narrow();
			}
			_this.setWindowzIndex();
		});

		/**************************任务栏事件*************************/

		addEvent(this.taskMenuCon,'click',function (){			//放大缩小按钮,点击下方任务栏，放大窗口
			_this.flag = !_this.flag;
			// if(_this.flag ){
			// 	_this.minimize();
			// }else{
			if(_this.isMax == 'mid'){
				_this.narrow();
			}else if(_this.isMax == 'max'){
				_this.maximize();
			}
			_this.setWindowzIndex();
			_this.currentWindow();
			// }
		});

		addEvent(this.taskMenuClose,'click',function (){		//关闭按钮
			_this.closeWindow();
		});

		/**************************窗口层级*************************/

		this.windowMenu.oncontextmenu = function (ev){
			var ev = ev||event;
			ev.cancelBubble = true;
			return false;
		};
		addEvent(this.windowMenu,'mousedown',function (){
			_this.setWindowzIndex();
			_this.clearMask();
			_this.currentWindow();
			_this.timer2 = setInterval(function (){
				_this.checkCurWindow();
			},30)
		});
		addEvent(this.contentMask,'mousedown',function (){
			_this.setWindowzIndex();
			_this.clearMask();
			_this.currentWindow();
			_this.timer2 = setInterval(function (){
				_this.checkCurWindow();
			},30)
		});
		addEvent(this.windowFrame,'mouseover',function (){
			if(_this.zIndex != zh_winzIndex){
				_this.addMask();
			}else{
				_this.clearMask();
			}
		});
	},
	createTask:function (){						//创建任务栏图标;
		console.log(this);
		this.taskMenu = document.createElement('div');
		this.taskMenuWrap = document.createElement('div');
		this.taskMenuCon = document.createElement('div');
		this.taskMenuMax = document.createElement('div');
		this.taskMenuClose = document.createElement('div');

		this.taskMenuCon.innerHTML = this.title;
//		this.taskMenuCon.innerHTML = this.
		this.taskMenu.id = this.windowId+'task';
		this.taskMenu.className = this.windowClass.taskMenu;
		this.taskMenuWrap.className = this.windowClass.taskMenuWrap;
		this.taskMenuCon.className = this.windowClass.taskMenuCon;
		this.taskMenuMax.className = this.windowClass.taskMax;
		this.taskMenuClose.className = this.windowClass.taskClose;

		this.taskMenuWrap.appendChild(this.taskMenuCon);
		this.taskMenuWrap.appendChild(this.taskMenuMax);
		this.taskMenuWrap.appendChild(this.taskMenuClose);
		this.taskMenu.appendChild(this.taskMenuWrap);
		if(this.oTask){
			this.oTask.appendChild(this.taskMenu);
		}

		if(this.windowId.indexOf('leftBar')!=-1){
			document.getElementById(this.windowId+"task").style.left='-100000px';
			document.getElementById(this.windowId+"task").style.position='absolute';
		}
		fnMove(this.taskMenu,{'opacity':100,'width':100});
		fnMove(this.thisWindow,{'opacity':0,'height':0,'top':viewH()});

		this.taskMenu.style.display = 'block';
	},
	show:function (){							//显示窗口
		console.log('show');
		var left = parseInt(this.iLeft)+(zh_windowCount-50)%150;
		var top = parseInt(this.iTop)+(zh_windowCount-50)%150;
		fnMove(this.thisWindow,{'opacity':100});
		setStyle(this.thisWindow,{'left':left+'px','top':top+'px','width':this.iWidth+'px','height':this.iHeight+'px'});
		this.iLeft = left;
		this.iTop = top;
		this.setWindowzIndex();
	},

	showTwo:function (ev,_this){							//第二次俩次窗口
		$("#"+ev.windowId+"task").find('.taskMenuCon').click();/*这里终于好了，好几把烦呀*/
		changeLeftIcon(_this.json.id)/*更换左侧图标*/
	},
	setWindowzIndex:function (){				//改变window层级
		if(this.windowId.indexOf('leftBarwindowleftIcon')!=-1){
			setStyle(this.thisWindow,{'z-index':zh_winzLeft++});
		}else{
			setStyle(this.thisWindow,{'z-index':zh_winzIndex++});
		}

		this.zIndex = zh_winzIndex;
	},

	currentWindow:function (){					//窗口设置为激活窗口
		addClass(this.windowMenu,this.windowClass.windowMenuactive);
		addClass(this.taskMenuCon,this.windowClass.taskMenuConHoverOn);
		this.windowFrame.focus();
	},
	cancelCurWindow:function (){				//取消窗口激活
		removeClass(this.windowMenu,this.windowClass.windowMenuactive);
		removeClass(this.taskMenuCon,this.windowClass.taskMenuConHoverOn);
	},
	checkCurWindow:function (){					//检测是否为当前窗口
		if(this.zIndex != zh_winzIndex){
			this.cancelCurWindow();
			clearInterval(this.timer2);
		}
	},
	addMask:function (){						//增加content遮罩
		setStyle(this.contentMask,{'display':'block'});
	},
	clearMask:function (){						//清除content遮罩
		setStyle(this.contentMask,{'display':'none'});
	},
	minimize:function (){						//窗口最小化;
		var _this = this;
		if(this.isMax == 'mid'){
			this.iHeight = 	this.thisWindow.offsetHeight;
			this.iLeft = this.thisWindow.offsetLeft;
			this.iTop = this.thisWindow.offsetTop;
			this.iWidth = this.thisWindow.offsetWidth;
		}
		fnMove(_this.taskMenu,{'opacity':100,'width':100});
		fnMove(_this.thisWindow,{'opacity':0,'height':0,'top':viewH()});
		this.isMax = this.isMax;
		_this.taskMenu.style.display = 'block';
		setTimeout(function (){
			_this.thisWindow.style.display = 'none';
		},50)
	},
	maximize:function (){						//窗口最大化;
		var _this = this;
		if(this.isMax == 'mid'){
			this.iHeight = 	this.thisWindow.offsetHeight ;
			this.iLeft = this.thisWindow.offsetLeft;
			this.iTop = this.thisWindow.offsetTop;
			this.iWidth = this.thisWindow.offsetWidth;
		}
		console.log(this);
		if(_this.windowId.indexOf('leftBarwindowleftIcon')!=-1){/*左边栏打开*/
			fnMove(_this.thisWindow,{'opacity':100,'height':viewH(),'width':viewW()-85,'left':85,'top':0});
		}else{
			fnMove(_this.thisWindow,{'opacity':100,'height':viewH()-95,'width':viewW()-85,'left':85,'top':60,});
		}
//		fnMove(_this.thisWindow,{'opacity':100,'height':viewH()-65,'width':viewW(),'left':0,'top':30});
		this.isMax = 'max';
		this.windowMaximize.className = this.windowClass.windowMax;
		_this.thisWindow.style.display = 'block';
	},
	narrow:function (){							//缩放窗口;
		var _this = this;
		fnMove(_this.thisWindow,{'opacity':100,'top':this.iTop,'left':this.iLeft,'height':this.iHeight,'width':this.iWidth});
		this.isMax = 'mid';
		this.windowMaximize.className = this.windowClass.windowMaximize;
		this.windowMaximize.style.top = '35px';
		_this.thisWindow.style.display = 'block';
	},
	closeWindow:function (){					//关闭窗口;
		var _this = this;
		zh_windowCount-=50;
		fnMove(_this.taskMenu,{'opacity':0,'width':0});
		fnMove(_this.thisWindow,{'opacity':0,'height':0});
		this.timer1 = setTimeout(function (){
			_this.oParent.removeChild(_this.thisWindow);
			_this.oTask.removeChild(_this.taskMenu);
		},500);
		setTimeout(function (){
			_this.taskMenu.style.display = 'none';
		},500);
	},
	resizeWindow:function (){					//改变窗口大小
		var _this = this;
		this.thisWindow.onmousedown = function (ev){
			if(_this.isMax != 'max'){
				_this.addMask();
				var ev = ev||event;
				var oPos = '',
					iX = ev.clientX,
					iY = ev.clientY+31,
					iLeft = _this.thisWindow.offsetLeft,
					iTop = _this.thisWindow.offsetTop,
					iWidth = _this.thisWindow.offsetWidth,
					iHeight = _this.thisWindow.offsetHeight,
					iRight = iLeft + iWidth,
					iBottom = iTop + iHeight+31;

				/*-------------以下判断为获得鼠标在窗口中的位置---------------*/
				/*-------------l:左，r:右，t:上，b:下---------------*/

				if(iX>=iLeft-5&&iX<=iLeft+5){
					oPos = 'l';
				}
				if(iX>=iRight-5&&iX<=iRight+5){
					oPos = 'r';
				}
				if(iY>=iTop-5&&iY<=iTop+5){
					oPos = 't';
				}
				if(iY>=iBottom-5&&iY<=iBottom+5){
					oPos = 'b';
				}
				if((iX>=iLeft-8&&iX<=iLeft+8)&&(iY>=iTop-8&&iY<=iTop+8)){
					oPos = 'tl';
				}
				if((iX>=iRight-8&&iX<=iRight+8)&&(iY>=iTop-8&&iY<=iTop+8)){
					oPos = 'tr';
				}
				if((iX>=iLeft-8&&iX<=iLeft+8)&&(iY>=iBottom-8&&iY<=iBottom+8)){
					oPos = 'bl';
				}
				if((iX>=iRight-8&&iX<=iRight+8)&&(iY>=iBottom-8&&iY<=iBottom+8)){
					oPos = 'br';
				}

				document.onmousemove = function (ev){
					var ev = ev||event,
						iW = iX-ev.clientX,
						iH = iY- ev.clientY-31,
						left = iLeft,
						top = iTop,
						width = iWidth,
						height = iHeight;

					/*-------以下所有判断，全为限制窗口活动范围.-------*/

					switch(oPos){
						case 'l':
							left = (iLeft - iW);
							width = (iWidth + iW);
							if(left<=0){
								left = 0;
								width = iLeft + iWidth;
							}
							if(width<=_this.minWidth){
								width = _this.minWidth;
								left = iLeft + iWidth-_this.minWidth;
							}
							break;
						case 't':
							top = (iTop - iH);
							height = (iHeight + iH);
							if(top<=31){
								top = 31;
								height = iHeight+iTop-top;
							}
							if(height<=_this.minHeight){
								height = _this.minHeight;
								top = iTop + iHeight-_this.minHeight;
							}
							break;
						case 'b':
							height = (iHeight - iH);
							if(height<=_this.minHeight){
								height = _this.minHeight;
							}
							break;
						case 'r':
							width = (iWidth - iW);
							if(width<=_this.minWidth){
								width = _this.minWidth;
							}
							break;
						case 'tl':
							left = (iLeft - iW);
							width = (iWidth + iW);
							top = (iTop - iH)
							height = (iHeight + iH);
							if(left<=0){
								left = 0;
								width = iLeft + iWidth;
							}
							if(top<=31){
								top = 31;
								height = iHeight+iTop-top;
							}
							if(width<=_this.minWidth){
								width = _this.minWidth;
								left = iLeft + iWidth-_this.minWidth;
							}
							if(height<=_this.minHeight){
								height = _this.minHeight;
								top = iTop + iHeight-_this.minHeight;
							}
							break;
						case 'tr':
							top = (iTop - iH)
							height = (iHeight + iH);
							width = (iWidth - iW);
							if(top<=31){
								top = 31;
								height = iHeight+iTop-top;
							}
							if(height<=_this.minHeight){
								height = _this.minHeight;
								top = iTop + iHeight-_this.minHeight;
							}
							if(width<=_this.minWidth){
								width = _this.minWidth;
							}
							break;
						case 'bl':
							left = (iLeft - iW);
							width = (iWidth + iW);
							height = (iHeight - iH);
							if(left<=0){
								left = 0;
								width = iLeft + iWidth;
							}
							if(width<=_this.minWidth){
								width = _this.minWidth;
								left = iLeft + iWidth-_this.minWidth;
							}
							if(height<=_this.minHeight){
								height = _this.minHeight;
							}
							break;
						case 'br':
							height = (iHeight - iH);
							width = (iWidth - iW);
							if(height<=_this.minHeight){
								height = _this.minHeight;
							}
							if(width<=_this.minWidth){
								width = _this.minWidth;
							}
							break;
					}
					if(width>=viewW()){
						width = viewW();
					}
					if(height>=viewH()-31){
						height = viewH()-31;
					}
					_this.thisWindow.style.left = left+'px';
					_this.thisWindow.style.top = top+'px';
					_this.thisWindow.style.width = width+'px';
					_this.thisWindow.style.height = height+'px';
					_this.iHeight = _this.thisWindow.offsetHeight;
					_this.iLeft = _this.thisWindow.offsetLeft;
					_this.iTop = _this.thisWindow.offsetTop;
					_this.iWidth = _this.thisWindow.offsetWidth;
				};
				document.onmouseup = function (){
					document.onmousemove = 	document.onmouseup = null;
					_this.clearMask();
				};
			};
			return false;
		}
	},
	drag:function (obj){						//窗口拖拽;
		var _this = this;
		console.log('drag');
		obj.onmousedown = function(ev) {
			console.log('onmousedown');
			var ev = ev || event;
			var flag = false;
			var disX = ev.clientX - parseInt(getStyle(_this.thisWindow,'left'));
			var disY = ev.clientY - parseInt(getStyle(_this.thisWindow,'top'));
			if (obj.setCapture) {
				obj.setCapture();
			}
			document.onmousemove = function(ev) {
				console.log('onmousemove');
				_this.addMask();
				var ev = ev || event;
				var iT = ev.clientY - disY;
				var iL = ev.clientX - disX;
				if(iT<=31){
					iT=31;
				};
				if(iT>=(viewH()+31-obj.offsetHeight)){
					iT=(viewH()+31-obj.offsetHeight);
				}
				if(_this.isMax == 'max' && flag == false){
//					iT = 31;
//					iL = 0;
					return false;/*最大化就不可以拖了*/
				}
				_this.thisWindow.style.left = iL + 'px';
				_this.thisWindow.style.top = iT + 'px';
				_this.iHeight = _this.thisWindow.offsetHeight;
				_this.iLeft = _this.thisWindow.offsetLeft;
			}
			document.onmouseup = function() {
				console.log('onmouseup');
				 document.onmouseup = document.onmousemove = null;
				 _this.clearMask();
				if (obj.releaseCapture) {
					obj.releaseCapture();
				}
			}
			ev.cancelBubble = true;
			return false;
		}
	}
}
